#!/bin/bash

# 获取脚本所在目录的路径
script_dir=$(dirname "$0")

# 获取解压的文件夹（假设是以 .tar.gz 格式压缩的文件）
tar_file=$(ls -l "$script_dir" | grep '^d' | awk '{print $9}')

#选择环境变量
switch_path(){
    case $1 in
    "hive") 
    echo "export HIVE_HOME=$2" >> /etc/profile
    echo 'export PATH=$PATH:$HIVE_HOME/bin' >> /etc/profile
    ;;
    "spark")  
    echo "export SPARK_HOME=$2" >> /etc/profile
    echo 'export PATH=$SPARK_HOME/bin:$SPARK_HOME/sbin:$PATH' >> /etc/profile
    ;;
    "hadoop")
    echo "export HADOOP_HOME=$2" >> /etc/profile
    echo 'export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH' >> /etc/profile
    echo 'export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop'>> /etc/profile
    echo 'export JAVA_LIBRAY_PATH=${HADOOP_HOME}/lib/native' >> /etc/profile
    echo 'export LD_LIBRARY_PATH=$JAVA_LIBRAY_PATH' >> /etc/profile
    ;;
    "jdk")
    echo "export JAVA_HOME=$2" >> /etc/profile
    echo 'export CLASSPATH=.:${JAVA_HOME}/lib' >> /etc/profile
    echo 'export PATH=${JAVA_HOME}/bin:$PATH' >> /etc/profile
    ;;
    "kafka")  echo 'kafka不需要配置环境变量'
    ;;
    "flink")
    echo "export FLINK_HOME=$2" >> /etc/profile
    echo 'export PATH=$PATH:$FLINK_HOME/bin' >> /etc/profile
    ;;
    "zookeeper")
    echo "export ZOOKEEPER_HOME=$2" >> /etc/profile
    echo 'export PATH=$PATH:$ZOOKEEPER_HOME/bin' >> /etc/profile
    ;;
    *)  echo '列表没有相关软件！请修改脚本'
    ;;
    esac
}



for file in $(ls "$script_dir/$tar_file")
do 
    path="$script_dir/$tar_file/$file"
    switch_path ${file} ${path}
done

source /etc/profile

echo "成功添加环境变量，请自己测试！"



